package com.yh.controller.admin;

import com.yh.config.OtherSource;
import com.yh.entity.LanguageVersion;
import com.yh.entity.Movie;
import com.yh.entity.MovieCountry;
import com.yh.entity.MovieType;
import com.yh.service.MovieService;
import com.yh.utils.Constants;
import com.yh.vo.CustomPageAndNavigate;
import com.yh.utils.ImgUrl;
import com.yh.utils.UploadAndDownload;
import com.yh.vo.MovieVO;
import com.yh.vo.ResponseVO;
import io.minio.MinioClient;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@RestController
@RequestMapping("/admin/movie")
public class MovieManageController {
    @DubboReference(check = false)
    private MovieService movieService;
    @Autowired
    private OtherSource otherSource;
    @Autowired
    private MinioClient minioClient;

    /**
     * 影片预热
     * 服务地址：http://localhost:8081/admin/movie/preheat
     */
    @RequestMapping("preheat")
    public ResponseVO moviePreheatToRedis() {
        Boolean preheatResult = movieService.moviePreheatToRedis();
        return preheatResult ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS)
                : ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
    }

    /**
     * 查询所有的电影信息；如果传递了分页参数，那么会自动将查询结果封装到自定义分页实例
     * 服务地址 http://localhost:8081/admin/movie/all
     */
    @RequestMapping("all")
    public ResponseVO findMovieAll(Integer pageNum, Integer pageSize) {
        List<Movie> movieList = movieService.findMovieAll();
        if (ObjectUtils.isEmpty(movieList)) {
            return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
        }
        if (ObjectUtils.isEmpty(pageNum) || ObjectUtils.isEmpty(pageSize)) {
            return ResponseVO.buildSuccessInstance(movieList);
        }
        // 分页
        CustomPageAndNavigate page = new CustomPageAndNavigate(pageNum, pageSize, movieList);
        return ResponseVO.buildSuccessInstance(page);
    }

    /**
     * 查询简单的影片信息
     * 服务地址 http://localhost:8081/admin/movie/movie?movieId=1
     */
    @RequestMapping("movie")
    public ResponseVO findMovieById(Integer movieId) {
        Movie movie = movieService.findMovieById(movieId);
        if (ObjectUtils.isEmpty(movie)) {
            return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
        }
        MovieVO vo = new MovieVO(movie);
        BeanUtils.copyProperties(movie, vo);
        vo.setMovie(movie);
        return ResponseVO.buildSuccessInstance(vo);
    }

    /**
     * 添加影片
     * 服务地址 http://localhost:8081/admin/movie/add
     */
    @RequestMapping("add")
    public ResponseVO addMovie(MovieVO movieVO) {
        MultipartFile movieFile = movieVO.getMovieFile();
        if (!ObjectUtils.isEmpty(movieFile)) {
            uploadImg(movieFile);
            // 补全影片海报的基本地址
            movieVO.setMoviePicture(ImgUrl.spliceBasicUrl(otherSource.movieFileUrl, movieFile.getOriginalFilename()));
        }
        int result = movieService.addMovie(movieVO);
        return ResponseVO.buildSuccessInstance(result);
    }

    /**
     * 修改影片信息
     * 服务地址 http://localhost:8081/admin/movie/update
     */
    @RequestMapping("update")
    public ResponseVO updateMovie(MovieVO movieVO) {
        MultipartFile movieFile = movieVO.getMovieFile();
        if (!ObjectUtils.isEmpty(movieFile)) {
            uploadImg(movieFile);
            // 补全影片海报的基本地址
            movieVO.setMoviePicture(ImgUrl.spliceBasicUrl(otherSource.movieFileUrl, movieFile.getOriginalFilename()));
        }
        int result = movieService.updateMovie(movieVO);
        return ResponseVO.buildSuccessInstance(result);
    }

    /**
     * 删除影片信息
     * 服务地址 http://localhost:8081/admin/movie/del
     */
    @RequestMapping("del")
    public ResponseVO deleteMovie(Integer movieId) {
        if (ObjectUtils.isEmpty(movieId)) {
            return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.DELETE_FAIL);
        }
        int result = movieService.deleteMovie(movieId);
        return ResponseVO.buildSuccessInstance(result);
    }

    /**
     * 查询所有的语言版本
     * 服务地址 http://localhost:8081/admin/movie/version/all
     */
    @RequestMapping("/version/all")
    public ResponseVO findVersionAll() {
        List<LanguageVersion> versionList = movieService.findLanguageVersionAll();
        if (ObjectUtils.isEmpty(versionList)) {
            return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
        }
        return ResponseVO.buildSuccessInstance(versionList);
    }

    /**
     * 查询所有的影片类型
     * 服务地址 http://localhost:8081/admin/movie/type/all
     */
    @RequestMapping("/type/all")
    public ResponseVO findMovieTypeAll() {
        List<MovieType> types = movieService.findMovieTypeAll();
        return !ObjectUtils.isEmpty(types) ? ResponseVO.buildSuccessInstance(types) :
                ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
    }

    /**
     * 添加影片类型
     * 服务地址 http://localhost:8081/admin/movie/add/type
     */
    @RequestMapping("/add/type")
    public ResponseVO addMovieType(@RequestBody MovieType type) {
        movieService.addMovieType(type);
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS);
    }

    /**
     * 查询所有的制片地区
     * 服务地址 http://localhost:8081/admin/movie/country/all
     */
    @RequestMapping("/country/all")
    public ResponseVO findMovieCountryAll() {
        List<MovieCountry> countryList = movieService.findMovieCountryAll();
        return !ObjectUtils.isEmpty(countryList) ? ResponseVO.buildSuccessInstance(countryList) :
                ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
    }

    /**
     * 添加制片区域
     * 服务地址 http://localhost:8081/admin/movie/add/country
     */
    @RequestMapping("/add/country")
    public ResponseVO addMovieCountry(@RequestBody MovieCountry country) {
        movieService.addMovieCountry(country);
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS);
    }

    /**
     * 上传图片
     */
    private void uploadImg(MultipartFile movieFile) {
        // 如果选中了图片就就执行图片的上传
        if (movieFile.getSize() > 0) {
            // 海报上传到远程服务器
            UploadAndDownload.uploadToMinIo(minioClient, otherSource.movieFileUrl, movieFile);
            // 将海报上传到本次磁盘（Win环境开发测试）
            UploadAndDownload.uploadToDisk(otherSource.uploadPath, movieFile);
            // 将海报上传到Nginx本地（Linux环境下的备用路径。MinIO服务器无法使用时的备用选择）
            UploadAndDownload.uploadToNginxLocalhost(otherSource.nginxPath, movieFile, otherSource.movieFileUrl);
        }
    }
}
